var prefix = ctx + "manage/adRecordSecondary";

$(function () {
    var options = {
        url: prefix + "/list",
        queryParams: queryParams,
        columns: [
            {
                field: 'usId',
                title: '用户Id'
            },
            {
                field: 'usNickname',
                title: '用户名'
            },
            {
                field: 'arsRealityMoney',
                title: '实际金额'
            },
            {
                field: 'arsChannel',
                title: '广告渠道',
                formatter: function (value, row, index) {
                    if (value === 'ad_pangle') {
                        return '穿山甲';
                    }
                    if (value === 'ad_ks') {
                        return '快手';
                    }
                    if (value === 'ad_gdt') {
                        return '优量汇';
                    }
                    if (value === 'ad_other') {
                        return '其他';
                    }
                }
            },
            {
                field: 'uvChannel',
                title: '渠道包',
                formatter: function (value, row, index) {
                    // 遍历所有选项
                    var textValue = "";
                    allOptionsChannel.each(function () {
                        if ($(this).val() === value || $(this).val() === value + "") {
                            textValue = $(this).text();
                        }
                    });
                    return textValue;
                }
            },
            {
                field: 'arsMediationRit',
                title: '广告位ID'
            },
            {
                field: 'arsPrimeRit',
                title: '聚合广告位'
            },
            {
                field: 'arsPosition',
                title: '广告位置',
                formatter: function (value, row, index) {
                    // 遍历所有选项
                    var textValue = "";
                    allOptions.each(function () {
                        if ($(this).val() === value || $(this).val() === value + "") {
                            textValue = $(this).text();
                        }
                    });
                    return textValue;
                }
            },
            {
                field: 'arsShowId',
                title: '唯一标识'
            },
            {
                field: 'createTime',
                title: '创建时间'
            },
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var actions = [];
                    actions.push('<a class="btn btn-primary btn-sm ' + editFlag + '" href="#" title="编辑" mce_href="#" onclick="edit(\'' + row.id + '\')"><i class="fa fa-edit"></i></a> ');
                    actions.push('<a class="btn btn-warning btn-sm ' + removeFlag + '" href="#" title="删除" onclick="remove(\'' + row.id + '\')"><i class="fa fa-remove"></i></a>');
                    return actions.join('');
                }
            }]
    };
    $.ryTable.init(options);
    var createTime = $('#createTime').val();
    var startCreatTime = createTime+" 00:00:00";
    var endTime = createTime+" 23:59:59";
    _ajax_query(prefix + "/statistics", {
        arsPositionCode: arsPositionCode,
        startTime: createTime === '' ? '' : startCreatTime,
        endTime: createTime === '' ? '' : endTime
    }, callback);
});

function queryParams(params) {
    var createTime = $('#createTime').val();
    var startCreatTime = createTime+" 00:00:00";
    var endTime = createTime+" 23:59:59";
    return {
        // 传递参数查询参数
        pageSize: params.limit,
        pageNum: params.offset / params.limit + 1,
        arsChannel: $("#arsChannel option:selected").val(),
        uvChannel: $("#uvChannel option:selected").val(),
        arsPosition: $("#arsPosition option:selected").val(),
        usId: $("#usId").val(),
        usNickname: $("#usNickname").val(),
        startGoldCoin: $("#startGoldCoin").val(),
        endGoldCoin: $("#endGoldCoin").val(),
        arsPositionCode: arsPositionCode,
        arsMediationRit: $("#arsMediationRit").val(),
        arsPrimeRit: $("#arsPrimeRit").val(),
        startTime: createTime === '' ? '' : startCreatTime,
        endTime: createTime === '' ? '' : endTime
    };
}

/*奖励记录-搜索*/
function search() {
    var createTime = $('#createTime').val();
    if(!createTime){
        $.modalAlert("请选择时间", modal_status.FAIL);
        return;
    }
    var startCreatTime = createTime+" 00:00:00";
    var endTime = createTime+" 23:59:59";
    $('.bootstrap-table').bootstrapTable('refresh', queryParams);
    var params = {
        // 传递参数查询参数
        arsChannel: $("#arsChannel option:selected").val(),
        uvChannel: $("#uvChannel option:selected").val(),
        arsPosition: $("#arsPosition option:selected").val(),
        usId: $("#usId").val(),
        usNickname: $("#usNickname").val(),
        startGoldCoin: $("#startGoldCoin").val(),
        endGoldCoin: $("#endGoldCoin").val(),
        arsPositionCode: arsPositionCode,
        arsMediationRit: $("#arsMediationRit").val(),
        arsPrimeRit: $("#arsPrimeRit").val(),
        startTime: createTime === '' ? '' : startCreatTime,
        endTime: createTime === '' ? '' : endTime
    };
    _ajax_query(prefix + "/statistics", params, callback);
}

function callback(result) {
    var arsRealityMoneySum = result.arsRealityMoneySum / 10000;
    $('label#arsRealityMoneySum').html("广告金币：" + formatNumber(result.arsRealityMoneySum) + "<span class='text-danger'>≈" + arsRealityMoneySum + "元</span>");
}